package com.meichis.yndms.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meichis.mcsappframework.adapter.recyclerview.MultiItemTypeAdapter;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.NoticeAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityNewExpressBinding;
import com.meichis.yndms.model.Notice;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.NewExpressVM;


import java.util.ArrayList;
import java.util.List;


/**
 * @author : create by  szh
 * @date : 2022/11/8 10:56
 * @desc :新品速递
 */

public class NewExpressActivity extends BaseActivity {

    ActivityNewExpressBinding binding;
    private NewExpressVM vm;
    private NoticeAdapter adapter;

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_new_express);
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        binding.setClick(new ClickEvent());
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(NewExpressVM.class);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("新品速递");
        binding.tvUnRead.setSelected(true);
        adapter = new NoticeAdapter(getActivity(), R.layout.item_notice, new ArrayList<>());
        binding.rcNotice.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rcNotice.setHasFixedSize(true);
        binding.rcNotice.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                if (!adapter.getDatas().get(position).isNoticeRead()) {
                    adapter.getDatas().get(position).setNoticeRead(true);
                    vm.UpdateNoticeRead(adapter.getDatas().get(position).getNoticeId());
                    util.setBooleanValue(SharePreferenceUtil.PREFERENCES_REFRESH, true);
                } else {
                    //当前点击页是已读界面
                    vm.isReadPage = true;
                }
                Intent intent = new Intent();
                intent.setClass(getActivity(), NoticeDetailActivity.class);
                intent.putExtra(MCode.NOTICE, adapter.getDatas().get(position));
                intent.putExtra(MCode.TYPE, adapter.getDatas().get(position).getNoticeType());
                intent.putExtra(MCode.TITLE, adapter.getDatas().get(position).getNoticeTitle());
                intent.putExtra(MCode.DATE, adapter.getDatas().get(position).getPublishDate());
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                return false;
            }
        });
        addObserver();
        vm.GetNewProductNotice();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (util.getBooleanValue(SharePreferenceUtil.PREFERENCES_REFRESH, false)) {
            util.remove(SharePreferenceUtil.PREFERENCES_REFRESH);
            //修改数据堆加
            vm.getReadNoticeList().getValue().clear();
            vm.getUnReadNoticeList().getValue().clear();
            vm.GetNewProductNotice();
        } else if (vm.getUnReadNoticeList().getValue().size() > 0 || !vm.isReadPage) {
            //只要不是从已读界面跳转到详情界面的任何界面回到此界面，都默认打卡未读界面
            binding.tvUnRead.setSelected(true);
            binding.tvRead.setSelected(false);
            setList(vm.getUnReadNoticeList().getValue());
        }
        vm.isReadPage = false;
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getNoticeList().observe(this, notices -> {
            vm.getReadNoticeList().getValue().clear();
            vm.getUnReadNoticeList().getValue().clear();
            for (Notice notice : notices) {
                if (notice.isNoticeRead()) {
                    vm.getReadNoticeList().getValue().add(notice);
                } else {
                    vm.getUnReadNoticeList().getValue().add(notice);
                }
            }
            if (binding.tvUnRead.isSelected()) {
                setList(vm.getUnReadNoticeList().getValue());
            } else {
                setList(vm.getReadNoticeList().getValue());
            }

        });
    }

    private void setList(List<Notice> notices) {
        adapter.setList(notices);
        if (notices.size() == 0) {
            binding.rcNotice.setVisibility(View.GONE);
            binding.tvNothing.setVisibility(View.VISIBLE);
        } else {
            binding.rcNotice.setVisibility(View.VISIBLE);
            binding.tvNothing.setVisibility(View.GONE);
        }
    }

    public class ClickEvent {

        public void onUnRead() {
            binding.tvUnRead.setSelected(true);
            binding.tvRead.setSelected(false);
            setList(vm.getUnReadNoticeList().getValue());
        }

        public void onRead() {
            binding.tvUnRead.setSelected(false);
            binding.tvRead.setSelected(true);
            setList(vm.getReadNoticeList().getValue());
        }
    }
}
